﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class TaskManage : Form
    {
        public TaskManage()
        {
            InitializeComponent();
            dataChanged = true;     // 界面载入后，刷新显示一次
            pictureBox_topMost_Click(null, null);
        }

        public static DailyForm dailyForm;      // 新建日志界面

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //taskSelect_List.addCheckIteam("任务一", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务二", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务三", Color.Black, true);
            //taskSelect_List.addCheckIteam("任务四", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务五", Color.Black, true);

            //taskSelect_List.addCheckIteam("任务十一", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务十二", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务十三", Color.Black, true);
            //taskSelect_List.addCheckIteam("任务十四", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务十五", Color.Black, true);


            //taskSelect_List.addCheckIteam("任务二十一", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务二十二", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务二十三", Color.Black, true);
            //taskSelect_List.addCheckIteam("任务二十四", Color.Black, false);
            //taskSelect_List.addCheckIteam("任务二十五", Color.Black, true);

            //taskSelect1.updateSize();
        }

        private void taskSelect1_CheckedChanged(object sender, EventArgs e, bool isChecked, string data)
        {
            //MessageBox.Show("子控件选中状态变动为:" + taskSelect1.Checked + ", 附加信息：" + taskSelect1.Data);
            //MessageBox.Show("子控件选中状态变动为:" + isChecked + ", 附加信息：" + data);
        }

        private void taskSelect1_SelectedChanged(object sender)
        {

        }

        private void taskSelect1_ContextMenuClick(object sender, EventArgs e, string menuName, checkIteam selectedIteam)
        {
            MessageBox.Show("调用菜单处理逻辑ContextMenuClick");
        }

        // 新建任务
        private void pictureBox_newTask_Click(object sender, EventArgs e)
        {
            //taskSelect_List.addNewIteam();

            // 显示新增日志界面
            if (dailyForm == null)
            {
                dailyForm = new DailyForm();
                dailyForm.Show();

                FormTool.AutoLocation_OnSide(this, dailyForm);
            }
        }

        // 设置为顶层显示
        private void pictureBox_topMost_Click(object sender, EventArgs e)
        {
            bool topmost = !this.TopMost;
            this.TopMost = topmost;
            if (topmost)
            {
                pictureBox_topMost.BackgroundImage = Properties.Resources.topMost2;
            }
            else pictureBox_topMost.BackgroundImage = Properties.Resources.topMost1;
        }

        // 关闭界面
        private void pictureBox_close_Click(object sender, EventArgs e)
        {
            this.Close();
            //FloatMenu.taskManage = null;
        }


        # region 界面信息显示逻辑

        // 定时更新界面显示信息
        private void timer_Tick(object sender, EventArgs e)
        {
            updateShowing(); // 刷新显示
        }

        // 标识urls对应的数据是否有变动，是否需要刷新显示
        public static bool dataChanged = true;
        private bool loadingShowing = true;

        // 更新列表的显示信息
        private void updateShowing()
        {
            //if (dataChanged) dataChanged = false;
            //else return;

            //string url = loginForm.urls[0];
            //string urlData = loginForm.webData[url];
            //if (loadingShowing || loginForm.diffrentStatus[url].Count > 0)
            //{
            //    showingData(taskSelect_List, urlData);  // 在当前界面中显示获取到的网页信息
            //    shwingDaily();                  // 更新日志信息的显示
            //    taskSelect_List.updateSize();   // 更新显示尺寸
            //}

            //loadingShowing = false;
        }

        // 在taskSelect中显示，特定格式的html数据信息
        private void showingData(TaskSelect taskSelect, String urlData)
        {
            //try
            //{
            //    List<string> head = Tool.getHeads(urlData);    // 获取列表标题
            //    List<string[]> rows = Tool.getBodys(urlData);  // 获取列表信息

            //    List<string[]> TaskInfo = Tool.getTaskInfo(head, rows);
            //    taskSelect.setTaskInfo(TaskInfo);      // 显示信息至任务管理界面  
            //}
            //catch (Exception e) { Console.WriteLine("showingData(): " + e.ToString()); }
        }

        # endregion



        # region 日志信息显示逻辑

        private bool dailyChanged = true;   // 标志日志是否需要更新显示
        // 显示日志信息到任务管理界面中
        private void shwingDaily()
        {
            if (dailyChanged) dailyChanged = false;
            else return;

            try
            {
                string daily = getDaily(loginForm.userName, Tool.DateString());   // 获取日志信息
                List<Data_Daily> data = Data_Daily.Iteams(daily);   // 解析日志信息
                AddTo(taskSelect_List, data);                       // 在任务列表中显示日志信息
            }
            catch (Exception e) { Console.WriteLine("shwingDaily(): " + e.ToString()); }
        }

        // 获取指定日期的日志信息
        private string getDaily(string uaccount, string makDay)
        {
            //string sql = "SELECT * FROM zt_daily WHERE uaccount='admin' AND markday='2016-05-13'";
            string sql = "SELECT * FROM zt_daily WHERE uaccount='"+ uaccount +"' AND markday='" + makDay + "'";
            string daily = Tool.Execute(sql);

            return daily;
        }

        // 用于记录日志信息
        Dictionary<string, Data_Daily> dailyInfo = new Dictionary<string, Data_Daily>();

        // 在taskSelect中显示，Data_daily数据信息
        private void AddTo(TaskSelect list, List<Data_Daily>  data)
        {
            //if (data == null) return;
            //list.isBatchAddIteams = true;
            //foreach (Data_Daily iteam in data)
            //{
            //    Color TextColor = Color.Black;

            //    String state = StatusStr(iteam.dstatus);
            //    Color noteColor = Tool.getColor(state);

            //    bool finished = Tool.getUnFinished(state);

            //    string key = "工作日志_" + iteam.id;
            //    list.addCheckIteam(iteam.work, TextColor, finished, key, state, noteColor);
            //    if (dailyInfo.Keys.Contains(key)) dailyInfo.Remove(key);    // 若日志信息中已有该日志则替换
            //    dailyInfo.Add(key, iteam);                                  // 记录日志信息
            //}
            //list.isBatchAddIteams = false;
            //list.updateSize();   // 更新显示尺寸
        }

        // 添加日志信息项到列表中
        public void AddDailyIteam(string Text, string status, string id)
        {
            //Color TextColor = Color.Black;

            //String state = StatusStr(status);
            //Color noteColor = Tool.getColor(state);

            //bool topMost = Tool.getUnFinished(state);

            //taskSelect_List.addCheckIteam(Text, TextColor, true, "工作日志_" + id, state, noteColor);
        }

        // 获取日志状态信息
        private string StatusStr(string i) { return StatusStr(int.Parse(i)); }
        private string StatusStr(int i)
        {
            try
            {
                string[] note = { "完成", "提前", "超时", "进行中"};
                return note[i];
            }
            catch (Exception e) { return "完成"; }
        }

        # endregion



        // 任务界面菜单选项处理逻辑
        private void taskSelect_List_ContextMenuClick(object sender, EventArgs e, string menuName, checkIteam selectedIteam)
        {
            if (selectedIteam == null) MessageBox.Show("请先选择要操作的任务");
            else
            {
                // ID = "工作日志_70"
                string ID = selectedIteam.Data;                 // 获取当前选中任务ID信息

                if (ID.StartsWith("工作日志"))
                {
                    if (loginForm.urlMode == 2)
                    {
                        if (menuName.Equals("查看")) HttpTool.openUrl("http://" + loginForm.serverIP + "/my-daily.html");
                        else if (menuName.Equals("编辑"))
                            //HttpTool.openUrl("http://" + loginForm.serverIP + "/daily-batchCreate-" + Tool.DateString() + ".html");
                            showDailyEdit(ID);
                    }
                }
                else
                {
                    if (menuName.Equals("查看")) ShowOnweb(ID);     // 在网页中查看
                    else if (menuName.Equals("编辑")) EditOnweb(ID); // 在网页中查看
                }
            }
        }

        // 显示日志信息编辑界面
        private void showDailyEdit(string key)
        {
            if (dailyInfo.Keys.Contains(key))
            {
                DailyForm form = DailyForm.Modify(dailyInfo[key], true);
                FormTool.AutoLocation_OnSide(this, form);
            }
        }


        // 打开任务信息页
        private void ShowOnweb(String ID)
        {
            string url = "http://" + loginForm.serverIP + "/zentao/task-view-" + ID + ".html";
            if (loginForm.urlMode == 1) url = "http://" + loginForm.serverIP + "/index.php?m=project&f=view&projectID=" + ID;
            else if (loginForm.urlMode == 2) url = "http://" + loginForm.serverIP + "/task-view-" + ID + ".html";

            HttpTool.openUrl(url);
        }

        // 打开bug信息页
        private void ShowBugOnweb(String ID)
        {
            string url = "http://" + loginForm.serverIP + "/zentao/bug-view-" + ID + ".html";
            if (loginForm.urlMode == 1) url = "http://" + loginForm.serverIP + "/index.php?m=bug&f=view&bugID=" + ID;
            HttpTool.openUrl(url);
        }

        // 打开任务信息对应的编辑页
        private void EditOnweb(String ID)
        {
            string url = "http://" + loginForm.serverIP + "/zentao/task-edit-" + ID + ".html";
            if (loginForm.urlMode == 1) url = "http://" + loginForm.serverIP + "/index.php?m=task&f=edit&taskID=" + ID;
            else if (loginForm.urlMode == 2) url = "http://" + loginForm.serverIP + "/task-edit-" + ID + ".html";

            HttpTool.openUrl(url);
        }

        private void TaskManage_MouseMove(object sender, MouseEventArgs e)
        {
        }

        // 控制无边框窗体的移动
        private void panel_content_MouseDown(object sender, MouseEventArgs e)
        {
            FormTool.MouseDown_MoveForm(this);
        }

        private void TaskManage_Load(object sender, EventArgs e)
        {
            FormTool.RestoreFormInfo(this); // 恢复界面信息
        }

        private void TaskManage_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (dailyForm != null) dailyForm.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        // 执行一次数据刷新，不管是否有数据变动
        private void timer_refresh_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            timer_refresh.Enabled = false;

            dataChanged = true;
            loadingShowing = true;
            dailyChanged = true;
            updateShowing();

            timer.Enabled = true;
        }

        // 执行一次界面刷新逻辑
        private void pictureBox_refresh_Click(object sender, EventArgs e)
        {
            timer_refresh_Tick(null, null);
        }

        // 延时执行刷新逻辑
        public void refresh()
        {
            timer_refresh.Enabled = true;
        }

        // 点击开始按钮，响应逻辑
        private void taskSelect_List_ButtonClicked(object sender, EventArgs e, checkIteam iteam)
        {
            //// 开始按钮界面
            //string url = "http://" + loginForm.serverIP + "/index.php?m=task&f=start&taskID=" + iteam.Data;
            //if (loginForm.urlMode == 2) url = "http://" + loginForm.serverIP + "/task-start-" + iteam.Data + ".html";

            //HttpTool.openUrl(url);

            ShowTaskOnBroswer(iteam.Data);      // 查看或编辑对应的任务
        }

        // 双击checkIteam时，跳转到任务对应的界面
        private void taskSelect_List_DoubleClick(object sender, EventArgs e)
        {
            checkIteam iteam = (checkIteam)sender;

            if (iteam.Data.StartsWith("工作日志"))
            {
                showDailyEdit(iteam.Data);
            }
            else
            {
                ShowTaskOnBroswer(iteam.Data);      // 查看或编辑对应的任务
            }

            //{
            //    // ID = "工作日志_70"
            //    string ID = selectedIteam.Data;                 // 获取当前选中任务ID信息

            //    if (ID.StartsWith("工作日志"))
            //    {
            //        if (loginForm.urlMode == 2)
            //        {
            //            if (menuName.Equals("查看")) HttpTool.openUrl("http://" + loginForm.serverIP + "/my-daily.html");
            //            else if (menuName.Equals("编辑"))
            //                //HttpTool.openUrl("http://" + loginForm.serverIP + "/daily-batchCreate-" + Tool.DateString() + ".html");
            //                showDailyEdit(ID);
            //        }
            //    }
            //    else
            //    {
            //        if (menuName.Equals("查看")) ShowOnweb(ID);     // 在网页中查看
            //        else if (menuName.Equals("编辑")) EditOnweb(ID); // 在网页中查看
            //    }
            //}
        }

        private void taskSelect_List_CheckedChanged(object sender, EventArgs e, checkIteam iteam)
        {
            if (iteam.Data.StartsWith("工作日志"))
                showDailyEdit(iteam.Data);
            else
                ShowTaskOnBroswer(iteam.Data);      // 查看或编辑对应的任务
        }


        // ---------------------------------------------------------------------------
        // 定义通用的链接跳转操作

        // 打开任务信息页
        public static void ShowTaskOnBroswer(String ID)
        {
            string url = "http://" + loginForm.serverIP + "/task-view-" + ID + ".html";
            HttpTool.openUrl(url);
        }

        // 打开任务信息页
        public static void ShowDemandOnBroswer(String ID)
        {
            string url = "http://" + loginForm.serverIP + "/task-view-" + ID + ".html";
            HttpTool.openUrl(url);
        }
        

    }
}
